﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GetFiles.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Returns the list of file names in a specified list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.File
{
    using System.Collections.Generic;

    using Collaboris.Saf.Actions.Wss.Entities;
    using Collaboris.Saf.Engine;
    using Collaboris.SharePoint.Wss.Files;
    using Collaboris.Utils;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;

    [CollaborisClass(
        Title = "Get Files",
        ReleaseStatus = ReleaseStatus.Beta,
        DisableDocumentation = false,
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Reads the list of files (and urls) specified in the ListFileInfo, based on a List Name and Filter Pattern.",
        Remarks = @"This action will also get the list of files during an 'Undo' operation by default.",
        UndoImplemented = true,
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C648}"
    )
    ]
    public class GetFiles : WSSActionBase<ListFileInfo>
    {
        #region Methods (6) 

        /// <summary>
        /// This method will CheckOut a File.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ListFileInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");

            ProcessStatus status = this.RunGetFiles(entity, context.Web, ProcessMode.Do);

            this.Trace.TraceMethodEnd("DoAction");

            return status;
        }

        /// <summary>
        /// This method will "Undo" the Current Checkout.
        /// </summary>
        /// <param name="context">
        /// IContext defining the current context.
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// ProcessStatus signalling to the Macro what its outcome was.
        /// </returns>
        protected override ProcessStatus UndoAction(SafContext context, ListFileInfo entity)
        {
            this.Trace.TraceMethodStart("UndoAction");
            
            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");

            ProcessStatus status = this.RunGetFiles(entity, context.Web, ProcessMode.Undo);

            this.Trace.TraceMethodEnd("UndoAction");

            return status;
        }

        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, ListFileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ListUrl, "ListItemInfo.ListUrl", ref errors);           
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }

        /// <summary>
        /// Checks out the specified the file as spec'd in <see cref="ListItemInfo"/>
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="web">The web.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        private ProcessStatus RunGetFiles(ListFileInfo entity, SPWeb web, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("RunGetFiles");
            this.Trace.TraceVerbose(
                "Getting the List of files from the list {0} using the filterpattern {1}.", 
                entity.ListUrl, 
                entity.FilterPattern);
                        
            List<string> fileNames = SPFileHelper.GetFileNames(TargetWeb, entity.ListUrl, entity.FilterPattern);
            entity.FileNames = new List<string>(fileNames);

            List<string> fileUrls = SPFileHelper.GetFileUrls(TargetWeb, entity.ListUrl, entity.FilterPattern);
            entity.FileUrls = new List<string>(fileUrls);            

            this.Trace.TraceMethodEnd("RunGetFiles");            

            return ProcessStatus.Success;

        }

        #endregion Methods 
    }
}